package team.stride.database.crud.results;

import java.util.ArrayList;
import java.util.List;

import team.stride.R;
import team.stride.database.StrideDB;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class BDResultsActivity extends Activity
{
	static final int MODIFY_DIALOG = 0;
	private List<ResultsItem> recordsList;
	private ListView listLV;
	private ResultsAdapter adapter;
	private Dialog modifDialog;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bd_results_layout);
		initDialog();
		listLV = (ListView) findViewById(R.id.resultsListView);

		recordsList = new ArrayList<ResultsItem>();
		adapter = new ResultsAdapter(this, recordsList);
		listLV.setAdapter(adapter);
	}

	@Override
	public void onResume()
	{
		super.onResume();
		recordsList.clear();
		Cursor cursor = managedQuery(StrideDB.CONTENT_URI_RESULTS, null, null, null, null);
		while (cursor.moveToNext())
		{
			final int id_index = cursor.getColumnIndex(StrideDB.ID_R);
			final int name_index = cursor.getColumnIndex(StrideDB.NAME_R);
			final int username_index = cursor.getColumnIndex(StrideDB.USERNAME_R);
			final int date_index = cursor.getColumnIndex(StrideDB.DATE_R);
			final int laps_index = cursor.getColumnIndex(StrideDB.LAPS_R);
			final int length_index = cursor.getColumnIndex(StrideDB.LENGTH_R);
			final int time_index = cursor.getColumnIndex(StrideDB.TIME_R);
			final int topSpeed_index = cursor.getColumnIndex(StrideDB.TOPSPEED_R);
			final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_R);

			int id = cursor.getInt(id_index);
			String name = cursor.getString(name_index);
			String username = cursor.getString(username_index);
			int date = cursor.getInt(date_index);
			float laps = cursor.getFloat(laps_index);
			float length = cursor.getFloat(length_index);
			int time = cursor.getInt(time_index);
			float topSpeed = cursor.getFloat(topSpeed_index);
			String details = cursor.getString(details_index);

			recordsList.add(new ResultsItem(id, name, username, date, laps, length, time, topSpeed, details));
		}
		cursor.close();
		adapter.notifyDataSetChanged();
	}

	private void initDialog()
	{
		modifDialog = new Dialog(this);
		modifDialog.setContentView(R.layout.bd_results_modify);
		modifDialog.setTitle("Modify a Result");
		modifDialog.setCancelable(true);
		modifDialog.setCanceledOnTouchOutside(true);
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		switch (id)
		{
		case MODIFY_DIALOG:
			return modifDialog;
		}
		return null;
	}

	public void showDialog(int modifyDialog, ResultsItem entry)
	{
		refreshDialog(entry);
		showDialog(MODIFY_DIALOG);
	}

	private void refreshDialog(final ResultsItem entry)
	{
		final EditText name = (EditText) modifDialog.findViewById(R.id.resultsInsertName);
		name.setText(entry.getName());
		final EditText details = (EditText) modifDialog.findViewById(R.id.resultsInsertDetails);
		details.setText(entry.getDetails());

		Button okBtn = (Button) modifDialog.findViewById(R.id.playlistsInsertOk);
		okBtn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				entry.setName(name.getText().toString());
				entry.setDetails(details.getText().toString());
				adapter.notifyDataSetChanged();

				final ContentResolver cr = v.getContext().getContentResolver();
				ContentValues values = new ContentValues();
				values.put(StrideDB.NAME_R, name.getText().toString());
				values.put(StrideDB.DETAILS_R, details.getText().toString());
				cr.update(StrideDB.CONTENT_URI_RESULTS, values, StrideDB.ID_R + " = " + entry.getId(), null);
				modifDialog.hide();
			}
		});

	}
}